package com.bajiao.admin.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bajiao.admin.base.AdminBaseController;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.constant.CacheConstant;
import com.bajiao.common.util.IDGeneratorUtils;
import com.bajiao.entity.system.Area;
import com.bajiao.service.system.IAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 行政区划
 * @Author: FanJq
 * @Date: 2024-10-17
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "行政区划")
@RestController
@RequestMapping("/system/area")
public class AreaController extends AdminBaseController<Area, IAreaService> {
    @Autowired
    private IAreaService areaService;

    @GetMapping(value = "/listByPid")
    public Result listByPid(@RequestParam("pid") String pid) {
        return Result.ok(areaService.listByPid(pid));
    }

    /**
     * @param area
     * @return
     * @description: 添加
     * @author: FanJq
     * @Date: 2024-10-17
     */
    @ApiOperation(value = "行政区划-添加", notes = "行政区划-添加")
    @PostMapping(value = "/add")
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.AREA_PID_LIST, key = "#area.pid", beforeInvocation = true),
            @CacheEvict(value = {CacheConstant.AREA_SSX_LIST}, beforeInvocation = true),
            @CacheEvict(value = {CacheConstant.AREA_CITY_LIST}, beforeInvocation = true)
    })
    public Result<?> add(@RequestBody Area area) {
        area.setId(IDGeneratorUtils.getStringId());
        if (area.getPid() != null) {
            Area pArea = areaService.getById(area.getPid());
            if (pArea != null) {
                if (pArea.getLevel() == 4) {
                    return Result.error("上级区域以为最底层，添加失败！");
                }
                area.setLevel(pArea.getLevel() + 1);
                area.setPids(pArea.getPids() + "," + area.getId());
            }
        } else {
            area.setLevel(1);
            area.setPid("0");
            area.setPids(area.getPid() + "," + area.getId());
        }
        areaService.save(area);
        return Result.ok("添加成功！");
    }

    /**
     * @param area
     * @return
     * @description: 编辑
     * @author: FanJq
     * @Date: 2024-10-17
     */
    @ApiOperation(value = "行政区划-编辑", notes = "行政区划-编辑")
    @PutMapping(value = "/edit")
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.AREA_PID_LIST, key = "#area.pid", beforeInvocation = true),
            @CacheEvict(value = {CacheConstant.AREA_SSX_LIST}, beforeInvocation = true),
            @CacheEvict(value = {CacheConstant.AREA_CITY_LIST}, beforeInvocation = true)
    })
    public Result<?> edit(@RequestBody Area area) {
        Area pArea = areaService.getById(area.getPid());
        if (pArea.getLevel() == 4) {
            return Result.error("上级区域以为最底层，修改失败！");
        }
        area.setLevel(pArea.getLevel() + 1);
        area.setPids(pArea.getPids() + "," + area.getId());
        areaService.updateById(area);
        return Result.ok("编辑成功!");
    }

    /**
     * @param id
     * @return
     * @description: 通过id删除
     * @author: FanJq
     * @Date: 2024-10-17
     */
    @ApiOperation(value = "行政区划-通过id删除", notes = "行政区划-通过id删除")
    @DeleteMapping(value = "/delete")
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.AREA_PID_LIST, key = "#pid", beforeInvocation = true),
            @CacheEvict(value = {CacheConstant.AREA_SSX_LIST}, beforeInvocation = true),
            @CacheEvict(value = {CacheConstant.AREA_CITY_LIST}, beforeInvocation = true)
    })
    public Result<?> delete(@RequestParam(name = "id", required = true) String id,@RequestParam(name = "pid", required = true) String pid) {
        //判断是否存在下级，存在下级不允许删除
        LambdaQueryWrapper<Area> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Area::getPid, id);
        if (areaService.count(lambdaQueryWrapper) > 0) {
            return Result.error("当前区域存在下级不允许删除!");
        }
        areaService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * @param id
     * @return
     * @description: 通过id查询
     @author: FanJq
     * @date: 2024-10-1011-17
     */
    @ApiOperation(value = "行政区划-通过id查询", notes = "行政区划-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        Area area = areaService.getById(id);
        return Result.ok(area);
    }

    /**
     * 获取省、市、乡列表数据
     *
     * @return
     */
    @GetMapping(value = "/listShengShiXiang")
    public Result listShengShiXiang() {
        return Result.ok(areaService.listShengShiXiang());
    }

    /**
     * 判断指定区域下是否存在下级如果存在不允许修改
     *
     * @return
     */
    @PostMapping(value = "/checkHasChild/{areaId}/{pid}")
    public Result checkHasChild(@PathVariable(value = "areaId") String areaId, @PathVariable(value = "pid") String pid) {
        Area area = areaService.getById(areaId);
        if (!StringUtils.equals(area.getPid(), pid)) {
            LambdaQueryWrapper<Area> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Area::getPid, areaId);
            if (areaService.count(lambdaQueryWrapper) > 0) {
                return Result.error("当前区域存在下一级不允许修改上级区域！");
            }
        }
        return Result.ok("验证成功");
    }

}
